﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebUI.Helper;

namespace WebUI.Con
{
    public class SaleCreditController : Controller
    {
        [Common.Attributes.Skip]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 挂账销售清单销售具体商品，需要清单CreditSaleNOID
        /// </summary>
        /// <returns></returns>

        public ActionResult btn_SaleGoods()
        {

            if (Request["CreditSaleNOID"] != null && Convert.ToInt32(Request["CreditSaleNOID"]) != 0)
            {
                Model.CreditSaleTD CreditSaleTD = new Model.CreditSaleTD();
                CreditSaleTD.CreditSaleNOID = Convert.ToInt32(Request["CreditSaleNOID"]);
                CreditSaleTD.CreditRoomID = Convert.ToInt32(Request["CreditRoomID"]);
                CreditSaleTD.CreditSaleNumber = Convert.ToInt32(Request["SaleNumber"]);
                CreditSaleTD.GoodsID = Convert.ToInt32(Request["SaleGoodsID"]);
                CreditSaleTD.CreditSaleTime = DateTime.Now;
                CreditSaleTD.UserID = OperateContext.Current.Usr.UserID;
                CreditSaleTD.IsExchange = Convert.ToBoolean(Request["IsExchange"]);
                CreditSaleTD.ShiftID = OperateContext.Current.Shift.ShiftID;

                int p = OperateContext.Current.BLLSession.ICreditSaleTDBLL.Add(CreditSaleTD);
                //实现挂账将对应销售商品库存货架减对应数量
                if (p == 1)
                {
                    Model.Good g_M = new Model.Good();
                    g_M.GoodsID = Convert.ToInt32(Request["SaleGoodsID"]);
                    g_M.Quntity = OperateContext.Current.BLLSession.IGoodBLL.GetListBy_NoTrack(s => s.GoodsID == CreditSaleTD.GoodsID).Select(s => s.Quntity).FirstOrDefault() - Convert.ToInt32(Request["SaleNumber"]);

                    OperateContext.Current.BLLSession.IGoodBLL.Modify(g_M, new string[] { "Quntity" });

                }
            }

            return Content("ok");
        }
        [Common.Attributes.Skip]
        /// <summary>
        /// 每桌挂账销售列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CreditSaleTD_List()
        {

            if (Request["CreditSaleNOID"] != null && Request["CreditSaleNOID"].ToString() != "")
            {
                int CreditSaleNOID = Convert.ToInt32(Request["CreditSaleNOID"]);
                //这单销售总价
               // decimal TotalMonery = OperateContext.Current.BLLSession.ICreditSaleTDBLL.GetListBy_NoTrack(s => s.CreditSaleNOID == CreditSaleNOID&&s.IsExchange==false).Select(s => (s.Good.SalePrice ?? 0) * (s.CreditSaleNumber ?? 0)).Sum(s => s);
                Model.ViewModel.CreditSaleTD_List_Head c = OperateContext.Current.BLLSession.ICreditSaleBLL.GetListBy_NoTrack(s => s.CreditSaleNOID == CreditSaleNOID).Select(s => new Model.ViewModel.CreditSaleTD_List_Head()
                {
                    CreditSaleNOID = s.CreditSaleNOID.ToString(),
                    TotalMoney = (s.CheckMoney ?? 0).ToString("0.00"),
                    IsCheck = (s.IsCheck ?? false) ? "已结账" : "未结账",
                    DiffTime = Common.DataHelper.DateDiff(s.CreditSaleBeginTime ?? DateTime.Now, DateTime.Now),
                    CreditSetup=s.CreditSetup.CreditName
                }).ToList().FirstOrDefault();
                ViewData["C_Head"] = c;
                
                var C_List = OperateContext.Current.BLLSession.ICreditSaleTDBLL.GetListBy(s => s.CreditSaleNOID == CreditSaleNOID).Select(s => new Model.ViewModel.CreditSaleTD()
                {
                    CreditSaleID = s.CreditSaleID,
                    CreditSaleTime = (s.CreditSaleTime ?? DateTime.Now).ToString("yy年MM月dd日hh时mm分"),
                    GoodsName = s.Good.GoodsName,
                    CreditSaleNumber = s.CreditSaleNumber ?? 0,
                    ShiftName = s.Shift.ShiftName,
                    IsExchange = (s.IsExchange ?? false) ? "兑奖" : "销售",
                    SalePrice=(s.Good.SalePrice??0).ToString("0.00"),
                    UserName=s.UserDB.NickName,
                    TotalPrice = ((s.Good.SalePrice ?? 0)*(s.CreditSaleNumber??0)).ToString("0.00")

                }).ToList();
               // var msg = new { TotalMonery, C_List };
                return PartialView("CreditSaleTD_List", C_List);
            }
            else
            {
                return PartialView("CreditSaleTD_List");
            }
            

        }
        /// <summary>
        /// 结账
        /// </summary>
        /// <returns></returns>
        public ActionResult CreditSaleTD_List_Checkout()
        {

            if (Request["CreditSaleNOID"] != null && Request["CreditSaleNOID"].ToString() != "")
            {
                int CreditSaleNOID = Convert.ToInt32(Request["CreditSaleNOID"]);
                Model.CreditSale Credit_Check = new Model.CreditSale();
                Credit_Check.CreditSaleNOID = CreditSaleNOID;
                Credit_Check.IsCheck = true;
                Credit_Check.CreditSaleCheckTime= DateTime.Now;
                Credit_Check.ShiftEndID = OperateContext.Current.Shift.ShiftID;
                Credit_Check.CheckUserID = OperateContext.Current.Usr.UserID;
                int p = OperateContext.Current.BLLSession.ICreditSaleBLL.Modify(Credit_Check, new string[] { "IsCheck", "CreditSaleCheckTime", "ShiftEndID","CheckUserID" });
                if (p == 1)
                {
                    //var sale = OperateContext.Current.BLLSession.ICreditSaleTDBLL.GetListBy_NoTrack(s => s.CreditSaleNOID == CreditSaleNOID).Select(s => new Model.SaleTD()
                    //{

                    //    SaleTime = DateTime.Now,
                    //    SaleUserID = s.UserID,
                    //    SaleShiftID = s.ShiftID,
                    //    SaleNumber = s.CreditSaleNumber,
                    //    SaleGoodsID = s.GoodsID,
                    //    SaleObject = s.CreditRoomID.ToString(),
                    //    IsExchange = s.IsExchange

                    //}).ToList();
                    //OperateContext.Current.BLLSession.ISaleTDBLL.Add(sale);
                    return Content("ok");
                }
                else
                {
                    return Content("Error");
                }
            }
            else
            {
                return Content("Error");
            }


        }
    }
}
